<HTML><HEAD><TITLE>subtract(+List1, +List2, ?Remainder)</TITLE>
</HEAD><BODY>[ <A HREF="index.html">library(lists)</A> | <A HREF="../../index.html">Reference Manual</A> | <A HREF="../../fullindex.html">Alphabetic Index</A> ]
<H1>subtract(+List1, +List2, ?Remainder)</H1>
Succeeds if Remainder is the list which contains those elements of List1
which are not in List2.


<DL>
<DT><EM>+List1</EM></DT>
<DD>List.
</DD>
<DT><EM>+List2</EM></DT>
<DD>List.
</DD>
<DT><EM>?Remainder</EM></DT>
<DD>List or variable.
</DD>
</DL>
<H2>Description</H2>
   Unifies Remainder with a list containing those elements of List1 which
   are not in List2.
<P>
   The definition of this Prolog library Predicate is:
<PRE>
subtract([], _, []).
subtract([Head|Tail], L2, L3) :-
        memberchk(Head, L2),
        !,
        subtract(Tail, L2, L3).
subtract([Head|Tail1], L2, [Head|Tail3]) :-
        subtract(Tail1, L2, Tail3).
</PRE>
   This predicate does not perform any type testing functions.
<P>
   This predicate works properly for set operations only, so repeated
   elements and variable elements should not be used.
	
<H3>Modes and Determinism</H3><UL>
<LI>subtract(+, +, -) is det
</UL>
<H3>Fail Conditions</H3>
   Fails if if Remainder does not unify with the list which contains those
   elements of List1 which are not in List2.


<H3>Resatisfiable</H3>
   No.
<H2>Examples</H2>
<PRE>
Success:
   subtract([1,2,3,4],[1],R).     (gives R=[2,3,4]).
   subtract([1,2,3],[3,4],R).     (gives R=[1,2]).
   subtract([1,1,2,3],[2],[1,1,3]).
Fail:
   subtract([1,1,2,3],[1],[1,2,3]). % Fails - List2 and
                                    % Remainder share elements



</PRE>
<H2>See Also</H2>
<A HREF="../../lib/lists/intersection-3.html">intersection / 3</A>, <A HREF="../../lib/lists/union-3.html">union / 3</A>
</BODY></HTML>
